<?php

namespace App\Http\Controllers;

use App\Exceptions\BaseException;
use App\Models\Purview;
use App\Services\PurviewService;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class AdminController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public $base;
    function __construct()
    {
        if(!empty(request()->route()->getAction()['as'])){
            $action_url = request()->route()->getAction()['as'];
            $actions = explode(".",$action_url);
            $controller = "";
            if(strpos($action_url,".") === false){
                $action = $actions[0];
            }else{
                $controller = $actions[0];
                $action = $actions[1];
            }
        }
        $this->base['controller'] = getActionName($controller);
        $this->base['action'] = getActionName($action);
    }

    public static function check_purview($model, $func = 'index') {
        $white = ['home'];
        if(in_array($model,$white)){
            return true;
        }
        $purview = unserialize(session('user_role')->purviewpids);
        //dd($purview);
        $flag = true;
        //判断该权限是否屏蔽
//        $this->load->model("purview/purview_model");
//        $this->purview_model->empty_self();
//        $flag = $this->purview_model->retrieve_is_use_by_pid($model);
        $purwhere[] = array('status','=',Purview::STATUS_SUCCESS);
        $purwhere[] = array('pid','=',$model);
        $flag = Purview::where($purwhere)->first();
        //解决产品模块权限问题(待优化)修改人(胡钰钒)
        /* if($model === "borrower"){
          $file_model=$this -> uri -> segment(1);
          if(strpos($file_model, "ajax_") !== false){
          $model=  $model."_".$func;
          }
          } */
        //echo $func;
        if ($flag) {
            if (isset($purview[$model])) {
                $new_arr = array_flip($purview[$model]);
                if (!isset($new_arr[$func])) {
                    $flag = false;
                }
            } else {
                $flag = false;
            }
        }
        if (!$flag) {
            echo "没有权限";exit;
            return response()->json([
                'code'      => 422001,
                'message'   => "没有权限",
            ]);
        } else
            return true;
    }

    public static function getMenu(){
        $ids_str = auth()->user()->role == 0 ? "" : session('user_role')->purviewids;
        //echo $ids_str;
        $purview = PurviewService::purviewAllByOpt(1,"",$ids_str);
        return self::_menuView($purview,session('user_role'));
    }
    public static function _menuView($purview){
        $purviewstr = '';
        if ($purview) {
            $newarr = array();
            //dd($purview);
            foreach ($purview as $item) {
                $newarr[$item->parent][$item->id] = $item;
            }
            //dd($newarr);
            if (isset($newarr[0])) {
                foreach ($newarr[0] as $item) {
                    $purviewstr .= '<li class="has-sub">
					<a href="javascript:;" class="">
					    <span class="icon-box"> <i class="' . $item->icon . '"></i></span> ' . $item->title . '
                        <span class="arrow"></span>
                    </a>
                    <ul class="sub">';
                    $purviewstr .= self::_formatPurview($newarr, $item);
                    $purviewstr .= '</ul></li>';
                }
            }
        }
        //echo $purviewstr;
        return $purviewstr;
    }
    private static function _formatPurview(&$newarr, $parent)
    {
        $purviewstr = '';
        if (isset($newarr[$parent['id']])) {
            foreach ($newarr[$parent['id']] as $item) {
                if ($item['url'] == '') {
                    $purviewstr .= '<li class="child" data-info="'.$item->id.'" onclick="childC($(this));"><a href="javascript:void(0)" >' . $item->title . '</a></li>';
                } else {
                    $purviewstr .= '<li class="child" data-info="'.$item->id.'" onclick="childC($(this));"><a class="" href="'.$item->url.'">' . $item->title . '</a></li>';
                }
                $purviewstr .= self::_formatPurview($newarr, $item);
            }
        }
        return $purviewstr;
    }
    /**
     * 成功响应
     * @param string $msg
     * @param array $data
     * @param int $code
     * @param string $redirect
     * @return \Illuminate\Http\JsonResponse
     */
    public function successResponse( $msg = '', $data = [], $code = 200, $redirect = 'parent'){
        return response()->json([
            'code'      => $code,
            'message'   => $msg,
            'data'      => $data,
            'redirect'  => $redirect
        ]);

    }
}
